package com.weetop.akly.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.weetop.akly.R;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.bean.VoteBean;
import com.weetop.akly.presenter.VoteDetailPresent;
import com.weetop.akly.view.VoteDetailView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VoteDetailActivity extends BaseActivity<VoteDetailPresent> implements VoteDetailView {

    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_data_title)
    TextView tvDataTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_vote_time)
    TextView tvVoteTime;
    @BindView(R.id.tv_my_vote)
    TextView tvMyVote;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.btn_fandui)
    Button btnFandui;
    @BindView(R.id.btn_zhichi)
    Button btnZhichi;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    private int voteId;
    private VoteBean voteInfo;

    @Override
    protected VoteDetailPresent createPresenter() {
        return new VoteDetailPresent(this, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vote_detail);
        ButterKnife.bind(this);
        setTitleInfo("投票详情");
        Intent intent = getIntent();
        voteId = intent.getIntExtra("vote_id", -1);
        if (voteId != -1) {
            mPresenter.getVoteInfo(voteId);
        }
    }

    @Override
    public void getVoteInfoSuccess(BaseModel<VoteBean> o) {
        voteInfo = o.getData();
        if (voteInfo == null) return;

        tvTitle.setText("投票标题：" + voteInfo.getTitle());
        tvStartTime.setText("开始时间：" + voteInfo.getStart_time());
        tvEndTime.setText("结束时间：" + voteInfo.getEnd_time());
        tvDesc.setText("问题描述：" + voteInfo.getDetails());
        if (voteInfo.getStatus() == 0) {
            llBottom.setVisibility(View.VISIBLE);
        } else {
            tvVoteTime.setVisibility(View.VISIBLE);
            tvMyVote.setVisibility(View.VISIBLE);
            tvVoteTime.setText("问题描述：" + voteInfo.getJoin_time());
            String tvMyVoteText = voteInfo.getStatus() == 1 ? "支持" : "反对";
            tvMyVote.setText("我的投票：" + tvMyVoteText);
        }


    }

    @Override
    public void doVoteSuccess(BaseModel<Boolean> o) {
        if (o.getData()) {
            ToastUtils.showShort("投票成功");
            finish();
        } else {
            ToastUtils.showShort(o.getMsg());
        }
    }

    @OnClick({R.id.btn_fandui, R.id.btn_zhichi})
    public void onViewClicked(View view) {
        // 选项：-1拒绝；1同意
        switch (view.getId()) {
            case R.id.btn_fandui:
                mPresenter.doVote(voteId, -1);
                break;
            case R.id.btn_zhichi:
                mPresenter.doVote(voteId, 1);
                break;
        }
    }
}
